﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;
using System.Collections;
using System.Collections.Generic;

namespace Model
{
    [Serializable()]
    public class TaiKhoanKTBOCollection : Csla.BusinessListBase<TaiKhoanKTBOCollection, TaiKhoanKTBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            TaiKhoanKTBO _TaiKhoanKTBO = TaiKhoanKTBO.NewChildTaiKhoanKTBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_TaiKhoanKTBO));
            _TaiKhoanKTBO.STT0 = this.Count + 1;
            Add(_TaiKhoanKTBO);
            return _TaiKhoanKTBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private TaiKhoanKTBOCollection()
        {
        }

        public static TaiKhoanKTBOCollection NewTaiKhoanKTBOCollection()
        {
            return new TaiKhoanKTBOCollection();
        }

        public static TaiKhoanKTBO NewTaiKhoanKTBO()
        {
            return TaiKhoanKTBO.NewChildTaiKhoanKTBO();
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection()
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "");
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("TaiKhoanKTID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }

        public static TaiKhoanKTBOCollection GetTaiKhoanKTBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "TaiKhoanKTID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<TaiKhoanKTBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("TaiKhoanKTBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    TaiKhoanKTBO objTaiKhoanKTBO = TaiKhoanKTBO.GetChildTaiKhoanKTBO(dr);
                    objTaiKhoanKTBO.STT0 = ++stt;
                    this.Add(objTaiKhoanKTBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            LogBOCollection logBOCollection = LogBOCollection.NewLogBOCollection();

            RaiseListChangedEvents = false;
            TaiKhoanKTBO child = null;
            int length = this.Count;
            int stt = 0;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                {
                    child.Insert();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "TaiKhoanKTID", TacVu.Them, "TaiKhoanKT", child.TaiKhoanKTID.Value));
                }
                else
                {
                    child.Update();
                    logBOCollection.Add(StaticUtil.CreateLog(child, "TaiKhoanKTID", (child.DELE.Value ? TacVu.Xoa : TacVu.Sua), "TaiKhoanKT", child.TaiKhoanKTID.Value));
                }

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
                else
                {
                    child.STT0 = ++stt;
                }
            }
            RaiseListChangedEvents = true;

            //-----
            logBOCollection = logBOCollection.Save();
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public TaiKhoanKTBO GetTaiKhoanKTBO(int _TaiKhoanKTID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].TaiKhoanKTID == _TaiKhoanKTID)
                    return this[i];

            return null;
        }

        public void Remove(int TaiKhoanKTID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].TaiKhoanKTID == TaiKhoanKTID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }

        public void Sort()
        {
            Sort(0, this.Count - 1);
        }

        private void Sort(int beg, int end)
        {
            if (end <= beg)
                return;
            else
            {
                int pivot = GetPivotPoint(beg, end);
                if (pivot > beg)
                    Sort(beg, pivot - 1);
                if (pivot < end)
                    Sort(pivot + 1, end);
            }
        }

        private string GetStrSort(TaiKhoanKTBO taiKhoanBO)
        {
            string s = taiKhoanBO.CapTaiKhoan.Value.ToString("000");
            if (taiKhoanBO.SoHieu1 == null)
                s += "000";
            else
                s += taiKhoanBO.SoHieu1.Value.ToString("000");

            if (taiKhoanBO.SoHieu2 == null)
                s += "0000";
            else
                s += taiKhoanBO.SoHieu2.Value.ToString("0000");

            if (taiKhoanBO.SoHieu3 == null)
                s += "00000";
            else
                s += taiKhoanBO.SoHieu3.Value.ToString("00000");

            return s;
        }

        private int GetPivotPoint(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            string sttPivot = GetStrSort(this[pivot]);
            string sttM = GetStrSort(this[m]);
            string sttN = GetStrSort(this[n]);
            TaiKhoanKTBO temp = null;

            while ((m < endPoint) && String.Compare(sttPivot, sttM) >= 0)
            {
                m++;
                sttM = GetStrSort(this[m]);
            }

            while ((n > begPoint) && String.Compare(sttPivot, sttN) <= 0)
            {
                n--;
                sttN = GetStrSort(this[n]);
            }

            while (m < n)
            {
                temp = this[m].Clone();
                this[m] = this[n];
                this[n] = temp;

                sttM = GetStrSort(this[m]);
                sttN = GetStrSort(this[n]);

                while (m < endPoint && String.Compare(sttPivot, sttM) >= 0)
                {
                    m++;
                    sttM = GetStrSort(this[m]);
                }

                while (n > begPoint && String.Compare(sttPivot, sttN) <= 0)
                {
                    n--;
                    sttN = GetStrSort(this[n]);
                }
            }

            if (pivot != n)
            {
                temp = this[n].Clone();
                this[n] = this[pivot];
                this[pivot] = temp;
            }

            return n;
        }
        #endregion Business Method
        //==========================================================================
    }
}
